//
//  BlacklistViewController.m
//  Molian
//
//  Created by 小蚕科技 on 15/5/13.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "BlacklistViewController.h"
#import "BlackListTableViewCell.h"
@interface BlacklistViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *userArr;
@property (nonatomic, assign) NSInteger removeInt;
@property (nonatomic, strong) Utils *uview;
@end

@implementation BlacklistViewController
- (UITableView *)tableView
{
    if (_tableView == nil) {
        self.automaticallyAdjustsScrollViewInsets = NO;
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, self.view.frame.size.width, self.view.frame.size.height-kNavigationBarHeight ) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[BlackListTableViewCell class] forCellReuseIdentifier:@"BlackListTableViewCell"];
        self.tableView.separatorColor = [UIColor clearColor];//分隔条颜色
        
    }
    
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = APPCOLORBACKGROUNDCOLOR(1);
    self.title = @"黑名单管理";
    [AVAnalytics event:@"黑名单管理页面访问"]; // 通知服务器一个点击事件。

    [self.view addSubview:self.tableView];
    self.uview = [[Utils alloc]init];
    [_uview viewControllerPrompt:self.view.bounds Text:@"没有黑名单~" Image:[UIImage imageNamed:@"xinlianren_nul"]];
    [self. view addSubview:_uview.viewPrompt];
    if ([self.userArr count] == 0) {
        _uview.viewPrompt.hidden = NO;
    }else{
        _uview.viewPrompt.hidden = YES;
    }
    [[EaseMob sharedInstance].chatManager asyncFetchBlockedListWithCompletion:^(NSArray *blockedList, EMError *error) {
        if (!error) {
            //获取成功
            self.userArr = [NSMutableArray array];
            //NSMutableArray *mblockedList = [NSMutableArray arrayWithArray:blockedList];
            
//            for (int i = 0; i<mblockedList.count; i++) {
//                NSString *oid = [mblockedList objectAtIndex:i];
//                if ([oid isEqualToString:@"NEW_CHAT_APPLY"]) {
//                    [mblockedList removeObjectAtIndex:i];
//                }
//            }
            
            
            for (NSString *uID in blockedList) {
                AVQuery *q = [MLUser query];
                [q whereKey:@"objectId" equalTo:uID];
                 [q orderByDescending:@"createdAt"];
                q.limit = 1000;
                [q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                    MLUser *u = [objects firstObject];
                    [_userArr addObject:u];
                    if ([self.userArr count] == 0) {
                        _uview.viewPrompt.hidden = NO;
                    }else{
                        [self.tableView reloadData];
                        _uview.viewPrompt.hidden = YES;
                    }
                }];
                
            }
            
        }
    } onQueue:nil];
    // Do any additional setup after loading the view.
}
#pragma mark - TableViewDelegate & TableViewDatasource
-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"BlackListTableViewCell";
    BlackListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if(!cell){
        cell = [[BlackListTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
    }
    MLUser *u= [self.userArr objectAtIndex:indexPath.row];
    cell.name = u.nickname;
    cell.placeholderImage = [UIImage imageNamed:@"common_koudai_icon"];
    cell.imageURL = [NSURL URLWithString: u.avatar.url];
    
    cell.removeBt.tag = 111+indexPath.row;
    [cell.removeBt addTarget:self action:@selector(clickBT:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
    
}
- (void)clickBT:(UIButton *)bt
{
    
    self.removeInt = bt.tag-111;
    HUD*hud = [[HUD alloc]init];
    [hud HUDImage:nil Title:@"移除黑名单" Content:@"确定要将Ta移除黑名单吗?" CANCEL:@"取消" Confirm:@"确定"];
    hud.Confirm  = ^{
        MLUser *u = [self.userArr objectAtIndex:self.removeInt];
        EMError *error = [[EaseMob sharedInstance].chatManager unblockBuddy:u.objectId];
        if (!error) {
            [HUD Prompt:@"移出成功"];
            for (int i = 0; i<self.userArr.count; i++) {
                MLUser *n = [self.userArr objectAtIndex:i];
                if ([n.objectId isEqualToString:u.objectId]) {
                    [self.userArr removeObjectAtIndex:i];
                    break;
                }
            }
            [self.tableView reloadData];
        }
    };
    hud.Cancel = ^{
        NSLog(@"取消");
    };
    [self.view addSubview:hud];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.userArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [BlackListTableViewCell tableView:tableView heightForRowAtIndexPath:indexPath];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
